<?php

namespace App\Domain\HealthHistory\Services;

use App\Domain\HealthHistory\Dao\HealthHistoryDao;

class HealthHistoryService
{
    public function findByDiseaseUid($uid)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByDiseaseUid($uid);
        $res = [
            'diseases' => $data['diseases'],
        ];
        return $res;
    }

    public function findByDiseaseUidAndWhere($param)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByDiseaseUidAndWhere($param);
        $res = [
            'diseases' => $data['diseases'],
        ];
        return $res;
    }

    public function findByAllergyUid($uid)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByAllergyUid($uid);
        $res = [
            'allergyMedicals' => $data['allergyMedicals'],
            'allergyFoods' => $data['allergyFoods'],
            'allergyContacts' => $data['allergyContacts'],
        ];
        return $res;
    }

    public function findByOperationUid($uid)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByOperationUid($uid);
        $res = [
            'names' => $data['name'],
            'values' => $data['date'],
            'hasOperation' => $data['hasOperation'],
        ];
        return $res;
    }

    public function Operation($param)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByOperationUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'hasOperation' => $param['hasOperation'],
                'date' => $param['values'],
                'name' => $param['names'],
            ];
            $data = $helthHistoryDao->updateOperation($data, $res);
        } else {
            $data = $helthHistoryDao->createOperation([
                'uid' => $param['memberUid'],
                'hasOperation' => $param['hasOperation'],
                'date' => $param['values'],
                'name' => $param['names'],
            ]);
        }
        return $data;
    }


    public function Disease($param)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByDiseaseUidAndWhere($param);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'diseases' => $param['diseases'],
                'where' => $param['where'],
            ];
            $data = $helthHistoryDao->updateDisease($data, $res);
        } else {
            $data = $helthHistoryDao->createDisease([
                'uid' => $param['memberUid'],
                'diseases' => $param['diseases'],
                'where' => $param['where'],
            ]);
        }
        return $data;
    }

    public function Allergy($param)
    {
        $helthHistoryDao = new HealthHistoryDao();
        $data = $helthHistoryDao->findByAllergyUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'allergyMedicals' => $param['allergyMedicals'],
                'allergyFoods' => $param['allergyFoods'],
                'allergyContacts' => $param['allergyContacts'],
            ];
            $data = $helthHistoryDao->updateAllergy($data, $res);
        } else {
            $data = $helthHistoryDao->createAllergy([
                'uid' => $param['memberUid'],
                'allergyMedicals' => $param['allergyMedicals'],
                'allergyFoods' => $param['allergyFoods'],
                'allergyContacts' => $param['allergyContacts'],
            ]);
        }
        return $data;
    }


}
